/*
		* Author:	 Sander
		* Date:		 12-apr-2011
		* Name:		 Plane
		* Version:	1.0
		*
		* This code is property of Sander blahblahblah copyright blahblah.
*/


package javaraytracer.Objects;

import javaraytracer.IntersectionInfo;
import javaraytracer.Materials.*;
import javaraytracer.Ray;
import javaraytracer.Vec3;

//PENDING:add function/property description
public class Plane extends Traceable
{

	public Vec3 normal;
	public double offset;

	public Plane(Vec3 n, double off, Material m)
	{
		normal = n;
		offset = off;
		material = m;
	}
	
	public IntersectionInfo intersect( Ray r )
	{
		double t = -(this.normal.dot( r.origin ) + this.offset) / (this.normal.dot( r.direction ));
		
		if(t > 0)
		{
			Vec3 location = r.origin.add(r.direction.mul(t));
			double distance = location.sub( r.origin ).length();

			IntersectionInfo ii = new IntersectionInfo( location, this.normal, (float)distance, this );
			return ii;
		}
		else
		{
			return new IntersectionInfo(false);
		}
	}

	public boolean hit( Ray r )
	{
		double t = -(this.normal.dot(r.origin) + this.offset) / (this.normal.dot(r.direction));

		if (t >= 0.0 && t <= 1.0)
		{
			return(true);
		}

		return false;
	}

	public Material getMaterial()
	{
		return material;
	}

	public Material getMaterial(IntersectionInfo info)
	{
		material.position(info);
		return material;
	}

}
